﻿using CarPCBATest.ViewModel;
using MahApps.Metro.Controls;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;

namespace CarPCBATest.View
{
    /// <summary>
    /// HomeView.xaml 的交互逻辑
    /// </summary>
    public partial class HomeView : UserControl
    {
        public HomeView()
        {
            InitializeComponent();
            var model = new HomeViewModel();
            this.DataContext = model;
            model.control = this.VmRenderControl;
  
            model.SNInputFocusSwitchAction = new System.Action<int>((index) =>
            {
                Application.Current.Dispatcher.Invoke(() => { 
                    TextBox focusBox = FindName("tbSN" + index.ToString()) as TextBox;
                    if (focusBox != null)
                    {
                        focusBox.Focus();
                        focusBox.SelectAll();
                    }
                });
            });
            model.SNInputVisibilityAction = new System.Action<int>((count) =>
            {
                Application.Current.Dispatcher.Invoke(() => {
                    if (count < 1 || count > 6)
                        return;
                    for (int i = 0; i < 6; i++)
                    {
                        MetroHeader currBox = FindName("mhSN" + i.ToString()) as MetroHeader;
                        if (currBox != null)
                        {
                            if (i < count)
                            {
                                currBox.Visibility = Visibility.Visible;
                            }
                            else
                            {
                                currBox.Visibility = Visibility.Collapsed;
                            }
                        }
                    }
                });

               
            });
        }
    }
}